<?php

namespace App\Http\Controllers\App\V1;

use App\Models\SociatyDB\SociatyUser;
use App\Repositories\LogRepository;
use Carbon\Carbon;
use App\Enums\ActionEnum;
use App\Traits\UserAction;
use App\Enums\ContentTypeEnum;
use App\Enums\ContentNameEnum;
use App\Jobs\Content\ContentJob;
use App\Services\Tools\ImageService;
use App\Http\Controllers\BaseController;
use App\Services\Tools\CurlService;
use App\Services\Tools\RedisService;
use Illuminate\Http\Request;

//引用http请求类
use App\Exceptions\BasicException;

//引入框架异常处理类
use App\Services\Tools\JwtService;

//引入jwt服务
use App\Services\Tools\FilterService;

//引入数美过滤服务
use App\Services\Tools\GeohashService;

//引入GeoHash服务
use App\Services\Tools\MapAreaService;

//引入地图服务
use Illuminate\Support\Facades\DB;

//引用DB
use App\Response\Response;

class UserController extends BaseController
{
    use UserAction;

    public function anchorList(Request $request)
    {
        //获取uid(登录强验证)
        $token = $request->header('token', '');
        $uid = JwtService::getUid($token, false);

        $user_id = $request->input('user_id', 0);

        if (empty($user_id)) {
            throw new BasicException(10010);
        }

        $data = app('repo_user')->getAnchorList($user_id, $uid);

        return Response::sendData($data);
    }

    public function anchorDetail(Request $request)
    {
        //获取uid(登录强验证)
        $token = $request->header('token', '');
        $uid = JwtService::getUid($token, false);

        $id = $request->input('id', 0);

        if (empty($id)) {
            throw new BasicException(10010);
        }

        $data = app('repo_user')->getAnchorDetail($id, $uid);

        return Response::sendData($data);
    }

    public function anchorCommentList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        //接收参数
        $param = $request->only(['id']);

        $perPage = 15;
        $page = $request->input('page', 1);

        $list = app('repo_user')->anchorCommentList($param, $page, $perPage);

        return Response::sendList($list, $param);
    }

    public function anchorLisenceArr(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $user_id = $request->input('user_id', 0);

        $list = app('repo_user')->getAnchorLisenceArr($user_id);

        return Response::sendData($list);
    }

    public function getUserBalence(Request $request)
    {
        $data = app('repo_order')->getUserBalence(UID);

        return Response::sendData($data);
    }

    public function userSkillList(Request $request)
    {
        //获取uid(登录强验证)
        $token = $request->header('token', '');
        $uid = JwtService::getUid($token, false);

        $user_id = $request->input('user_id', 0);

        if (empty($uid) && empty($user_id)) {
            throw new BasicException(30034);
        }

        $id = !empty($user_id) ? $user_id : $uid;

        $data = app('repo_user')->userSkillList($id);

        return Response::sendData($data);
    }

    public function privateOrder(Request $request)
    {
        $user_id = $request->input('user_id', 0);

        if (empty($user_id)) {
            throw new BasicException(30036);
        }

        $data = app('repo_user')->getPrivateOrder($user_id, UID);

        return Response::sendData($data);
    }

    public function getOrderMessage(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $perPage = 15;
        $page = $request->input('page', 1);

        $list = app('repo_user')->getMessageList(UID, $page, $perPage);

        return Response::sendList($list);
    }

    public function readOrderMessage(Request $request)
    {
        app('repo_user')->readOrderMessage(UID);

        return Response::sendData(true);
    }

    public function signinProcess()
    {
        $result = app('repo_user')->signin(UID, 1);

        return Response::sendData($result);
    }

    public function createMicroBlog(Request $request)
    {
        $this->checkUserAction(UID, ContentNameEnum::MICROBLOG);

        $v = app('validator')->make(
            $request->all(),
            [
                'content' => 'filled|min:1|max:500',
                'image' => 'filled|min:1|max:2000',
                'type' => 'filled|in:1,2,3',
                'cover' => 'filled|min:1|max:2000',
                'lat' => 'filled|numeric',
                'lng' => 'filled|numeric',
                'position' => 'filled|min:1|max:50',
                'privacy' => 'filled|in:1,2,3',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $content = $request->input('content', '');

        $image = $request->input('image', '');

        $cover = $request->input('cover', '');

        $lat = $request->input('lat', 0);

        $lng = $request->input('lng', 0);

        $position = $request->input('position', '');

        $privacy = $request->input('privacy', 1);

        $type = $request->input('type', 1);  //动态媒体类型1单纯的图片,2视频,3心愿动态

        if ($type == 3) {
            $wish_id = $request->input('wish_id', 0);
            $wish = app('model_mall_wishlist')->where(['user_id' => UID, 'id' => $wish_id])->first();
            if (!$wish) {
                throw new BasicException(330029);
            }
            $product = app('model_mall_products')->where('id', $wish->product_id)->first();
            if (!$product) {
                throw new BasicException(62001);
            }

            return Response::sendData(true);
        }


        if (empty($content) && empty($image)) {
            throw new BasicException(30048);
        }

        $verify = 2;

        $jobData = [
            'user_id' => UID,
            'content_type' => ContentTypeEnum::MICROBLOG,
            'content_name' => ContentNameEnum::MICROBLOG,
            'content' => $content,
        ];
        $pictures = [];
        //校验动态图片
        if (!empty($image)) {
            $image_arr = json_decode($image, true);

            if (!is_array($image_arr)) {
                throw new BasicException(30042);
            }

            if ($type == 1) {
                $verify = 2;

                //上传的是图片
                if (count($image_arr) > 9) {
                    throw new BasicException(30043);
                }
                //进行数美图片过滤  todo:迁移到新的渠道
                foreach ($image_arr as $val) {
                    if (isset($val['url'])) {
                        //上传的必须都是图片
                        if (!in_array(pathinfo($val['url'], PATHINFO_EXTENSION),
                            ['png', 'jpeg', 'jpg', 'gif', 'JPG', 'JPEG'])) {
                            throw new BasicException(30051);
                        }

//                        $filter_data = ['img' => $val['url'], 'tokenId' => UID, 'channel' => 'microblog'];
//                        $res = FilterService::checkImage($filter_data);
//
//                        if ($res) {
//                            throw new BasicException(30044);
//                        }
                        $pictures[] = $val['url'];
                    }
                }
                $jobData['pictures'] = json_encode($pictures);
            } else {
                $verify = 1;

//                throw new BasicException(30064);

                //上传的是视频
                if (count($image_arr) > 1) {
                    throw new BasicException(30049);
                }

                //视频进行数美过滤
                foreach ($image_arr as $val) {
                    if (isset($val['url'])) {
                        $pictures[] = $val['url'];
                    }
                }

                //检查封面
                if (empty($cover)) {
                    throw new BasicException(30050);
                }

                //进行封面过滤
                $cover_arr = json_decode($cover, true);

                if (!is_array($cover_arr)) {
                    throw new BasicException(30042);
                }
                if (!empty($pictures)) {
                    $jobData['videos'] = json_encode($pictures);
                }
            }
        }

        $GeoHash = new GeohashService();
        //整理参数
        $param = [
            'content' => $content,
            'img_arr' => $image,
            'cover' => $cover,
            'lat' => $lat,
            'lng' => $lng,
            'position' => $position,
            'type' => $type,
            'privacy' => $privacy,
            'verify' => $verify,
            'geohash' => $GeoHash->encode($lat, $lng),
        ];

        $id = app('repo_user')->createMicroBlog($param);

        $jobData['key_id'] = $id;
        $contentQueueName = env('CONTENT_QUEUE_NAME');
        dispatch(new ContentJob($jobData))->onQueue($contentQueueName);

        return Response::sendData(true);
    }

    public function likeMicroBlog(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $blog_id = $request->input('id');

        //检查当前动态是否可以点赞
        $blog = app('repo_user')->getMicroBlogDetail($blog_id);

        if (empty($blog) || $blog->status == 2) {
            throw new BasicException(30052);
        }

        //查询好友关系,点赞权限   //todo:需更新
        if (($blog->privacy == 3) && ($blog->user_id != UID)) {
            throw new BasicException(30053);
        }

        //查询是否点过赞
        $like_detail = app('repo_user')->getLikeDetail($blog_id, 1);

        if (!empty($like_detail)) {
            throw new BasicException(30055);
        }

        //执行点赞操作  todo:迁移缓存,异步入库
        DB::beginTransaction();
        try {
            app('repo_user')->doLikeAction([
                'blog_id' => $blog_id,
                'user_id' => UID,
                'add_time' => time(),
                'type' => 1,
            ]);
            app('model_microblog')->where('id', $blog_id)->increment('like_num');
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(90026);
        }

        if ($blog->user_id != UID) {
            //发送点赞消息
            $message_data = [
                'user_id' => $blog->user_id,
                'uid' => UID,
                'type' => 1,
                'blog_id' => $blog->id,
                'add_time' => time(),
            ];
            app('repo_user')->createMicroMessage($message_data);
        }


        return Response::sendData(true);
    }

    public function deleteLike(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
                'type' => 'required|in:1,2',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id');

        $type = $request->input('type', 1);

        DB::beginTransaction();
        try {
            app('model_microblog_like')->where(['blog_id' => $id, 'user_id' => UID, 'type' => $type])->delete();
            if ($type == 1) {
                app('model_microblog')->where('id', $id)->decrement('like_num');
                app('model_microblog_message')->where(['blog_id' => $id, 'uid' => UID, 'type' => 1])->delete();
            } else {
                app('model_microblog_comment')->where('id', $id)->decrement('like_num');
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(90027);
        }

        return Response::sendData(true);
    }

    public function commentMicroBlog(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
                'content' => 'required|min:1|max:100',
                'uid' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $blog_id = $request->input('id', 0);
        $content = $request->input('content', '');
        $uid = $request->input('uid', 0);

        //检查blog
        $blog = app('repo_user')->getMicroBlogDetail($blog_id);
        if (empty($blog) || $blog->status != 1) {
            throw new BasicException(30056);
        }

        //检查被评论人是否存在
        if ($uid !== 0) {
            $user = app('repo_user')->getUserDetail($uid);
            if (empty($user)) {
                throw new BasicException(30057);
            }
        }

        //评论内容进行数美过滤
        $res = FilterService::checkNickname($content, UID);
        if ($res) {
            throw new BasicException(30058);
        }

        DB::beginTransaction();
        try {
            $comment = app('model_microblog_comment')->create([
                'blog_id' => $blog_id,
                'content' => $content,
                'user_id' => UID,
                'uid' => $uid,
                'add_time' => time(),
            ]);
            app('model_microblog')->where('id', $blog_id)->increment('comment_num');
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(90028);
        }

        //非对自己操作才发送消息
        if ($uid == 0) {
            if ($blog->user_id != UID) {
                //发送一条
                $message_data = [
                    'user_id' => $blog->user_id,
                    'uid' => UID,
                    'type' => 2,
                    'blog_id' => $blog->id,
                    'add_time' => time(),
                    'content_id' => $comment->id,
                ];
                app('repo_user')->createMicroMessage($message_data);
            }
        } else {
            if ($blog->user_id == $uid || $blog->user_id == UID) {
                //用户回复博主,只收到一条
                $message_data = [
                    'user_id' => $uid,
                    'uid' => UID,
                    'type' => 2,
                    'blog_id' => $blog->id,
                    'add_time' => time(),
                    'content_id' => $comment->id,
                ];

                app('repo_user')->createMicroMessage($message_data);
            } else {
                $message_data_one = [
                    'user_id' => $blog->user_id,
                    'uid' => UID,
                    'reply_uid' => $uid,
                    'type' => 2,
                    'blog_id' => $blog->id,
                    'add_time' => time(),
                    'content_id' => $comment->id,
                ];

                app('repo_user')->createMicroMessage($message_data_one);
                if ($uid != $blog->user_id) {
                    $message_data_two = [
                        'user_id' => $uid,
                        'uid' => UID,
                        'reply_uid' => $uid,
                        'type' => 2,
                        'blog_id' => $blog->id,
                        'add_time' => time(),
                        'content_id' => $comment->id,
                    ];

                    app('repo_user')->createMicroMessage($message_data_two);
                }
            }
        }

        return Response::sendData(true);
    }

    public function deleteComment(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id');

        $comment = app('repo_user')->getCommentDetail($id);
        if (empty($comment) || $comment->status != 1) {
            throw new BasicException(30059);
        }

        if ($comment->user_id != UID) {
            throw new BasicException(30060);
        }

        DB::beginTransaction();
        try {
            app('model_microblog_comment')->where('id', $id)->where('user_id', UID)->update(['status' => 2]);
            app('model_microblog')->where('id', $comment->blog_id)->decrement('comment_num');
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(90025);
        }

        return Response::sendData(true);
    }

    public function likeMicroBlogComment(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id');

        //检查当前评论
        $comment = app('repo_user')->getCommentDetail($id);
        if (empty($comment) || $comment->status != 1) {
            throw new BasicException(30059);
        }

        //查询是否点过赞
        $like_detail = app('repo_user')->getLikeDetail($id, 2);

        if (!empty($like_detail)) {
            throw new BasicException(30055);
        }

        //执行点赞操作  todo:迁移缓存,异步入库
        DB::beginTransaction();
        try {
            app('repo_user')->doLikeAction(['blog_id' => $id, 'user_id' => UID, 'add_time' => time(), 'type' => 2]);
            app('model_microblog_comment')->where('id', $id)->increment('like_num');
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(90026);
        }

        return Response::sendData(true);
    }

    public function updateCommentPrivacy(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
                'privacy' => 'required|in:1,2,3',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id');
        $privacy = $request->input('privacy', 1);

        //检查blog
        $blog = app('repo_user')->getMicroBlogDetail($id);
        if (empty($blog) || $blog->status != 1) {
            throw new BasicException(30056);
        }
        if ($blog->user_id != UID) {
            throw new BasicException(30061);
        }

        app('repo_user')->updateMicroBlog($id, ['privacy' => $privacy]);

        return Response::sendData(true);
    }

    public function getBottomList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required',
                'type' => 'required|in:1,2',
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        //接收参数
        $param = $request->only(['id', 'type']);

        $perPage = 15;
        $page = $request->input('page', 1);

        $list = app('repo_user')->getBottomList($param, $page, $perPage);

        return Response::sendList($list, $param);
    }

    public function deleteMicroBlog(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id', 0);

        app('repo_user')->deleteMicroBlog($id);

        return Response::sendData(true);
    }

    public function getMicroBlogList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'required|numeric',
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        //接收参数
        $param = $request->only(['user_id']);

        $perPage = 15;
        $page = $request->input('page', 1);

        $list = app('repo_user')->getMicroBlogList($param, $page, $perPage);

        return Response::sendList($list, $param);
    }

    public function getMicroBlogIndexList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3',
                'search' => 'filled|min:1',
                'lng' => 'filled|numeric',
                'lat' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        //接收参数
        $param = $request->only(['type', 'search', 'lng', 'lat']);

        $param['lng'] = !empty($param['lng']) ? $param['lng'] : 0;
        $param['lat'] = !empty($param['lat']) ? $param['lat'] : 0;

        $perPage = 15;
        $page = $request->input('page', 1);

        //判断是否是过审版本
        if (functionLimit()) {
            $list = app('repo_user')->getMicroBlogIndexListNew($param, $page, $perPage);
        } else {
            $list = app('repo_user')->getMicroBlogIndexList($param, $page, $perPage);
        }


        return Response::sendList($list, $param);
    }

    public function shareSuccess(Request $request)
    {
        $token = $request->header('token', '');
        $uid = JwtService::getUid($token, true);

        return Response::sendData(true);
    }

    public function getMicroBlogDetail(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
                'lat' => 'filled|numeric',
                'lng' => 'filled|numeric',
                'type' => 'filled|in:1,2,3',

            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $id = $request->input('id', 0);

        $param = [];

        $param['lat'] = $request->input('lat', 0);
        $param['lng'] = $request->input('lng', 0);
        $param['type'] = $request->input('type', 1);

        $detail = app('repo_user')->getBlogDetail($id, $param);

        return Response::sendData($detail);
    }

    public function getMicroBlogMessage(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3',
                'page' => 'filled|numeric',

            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        //接收参数
        $param = $request->only(['type']);

        $perPage = 15;
        $page = $request->input('page', 1);

        $list = app('repo_user')->getMicroBlogMessage($param, $page, $perPage);

        return Response::sendList($list, $param);
    }

    public function readBlogMessage(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $type = $request->input('type', 1);
        app('model_microblog_message')->where([
            'user_id' => UID,
            'type' => $type,
            'status' => 1,
        ])->update(['status' => 2, 'read_time' => time()]);

        return Response::sendData(true);
    }

    public function userFollow(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'user_id' => 'required|numeric',
                'type' => 'filled|in:1,2',        //关注接口的类别1是关注2是取消关注
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $user_id = $request->input('user_id', 0);

        $type = $request->input('type', 1);

        if ($user_id == UID) {
            throw new BasicException(50003);
        }

        $count = app('model_user')->whereIn('user_id', [UID, $user_id])->count();

        if ($count != 2) {
            throw new BasicException(50001);
        }

        $follow_record = app('model_follow')->where(['user_id' => UID, 'followed_user' => $user_id])->first();


        //进行业务拆解
        if ($type == 1) {
            if ($follow_record) {
                throw new BasicException(50002);
            }

            //添加关注关系
            $follow_data = [
                'user_id' => UID,
                'followed_user' => $user_id,
                'add_time' => time(),
            ];

            $res = app('model_follow')->create($follow_data);
            if (!$res) {
                throw new BasicException(50007);
            }

            //------发送关注消息-----
            //检查有没有此人的关注消息,有不重复发送
            $message_detail = app('model_microblog_message')->where([
                'user_id' => $user_id,
                'uid' => UID,
                'type' => 3,
            ])->first();

            if (empty($message_detail)) {
                $message_data = [
                    'user_id' => $user_id,
                    'uid' => UID,
                    'type' => 3,
                    'add_time' => time(),
                ];
                app('repo_user')->createMicroMessage($message_data);
            }

            //粉丝通知部分
            $user_config = app('model_user_config')->where('user_id', $user_id)->select('fans')->first();
            if (isset($user_config->fans) && ($user_config->fans == 1)) {
                //开启了粉丝通知,发送消息
                $nickname = app('model_user_data')->where('user_id', UID)->select('nickname')->first();
                $nickname = !empty($nickname) ? $nickname->nickname : '';
                $content = "您有新的粉丝" . $nickname . "关注了你";
                app('model_user_news')->create(['user_id' => $user_id, 'title' => '关注消息', 'content' => $content,
                    'add_time' => Carbon::now()->timestamp,
                ]);
                app('redis')->del('user:news:countStat:' . $user_id);
                app('redis')->del('user:news:infoList:' . $user_id);
            }
        } else {
            if (empty($follow_record)) {
                throw new BasicException(50009);
            }

            //解除关注关系
            $res = app('model_follow')->where(['user_id' => UID, 'followed_user' => $user_id])->delete();
            if (!$res) {
                throw new BasicException(50010);
            }
        }

        //查询是否相互关注
        $follow_other = app('model_follow')->where(['user_id' => $user_id, 'followed_user' => UID])->first();

        if (!empty($follow_other)) {
            if ($type == 1) {
                //关注
                //创建好友关系
                app('model_friend')->create(['user_id' => $user_id, 'friend_id' => UID, 'add_time' => time()]);
                app('model_friend')->create(['user_id' => UID, 'friend_id' => $user_id, 'add_time' => time()]);
                app('rongcloud')->whitelistADD(UID, [$user_id]);
                app('rongcloud')->whitelistADD($user_id, [UID]);
                RedisService::addWhitelist($user_id, UID);
                RedisService::addWhitelist(UID, $user_id);
                RedisService::addFriend($user_id, UID);
            } else {
                //取消关注
                app('model_friend')->where(['user_id' => $user_id, 'friend_id' => UID])->delete();
                app('model_friend')->where(['user_id' => UID, 'friend_id' => $user_id])->delete();
                RedisService::delFriend($user_id, UID);
                $sociaty_user_id = SociatyUser::query()->where(['user_id' => UID, 'is_speak' => 2])->value('id');
                $sociaty_to_user_id = SociatyUser::query()->where(['user_id' => $user_id, 'is_speak' => 2])->value('id');
                if (empty($sociaty_user_id) && empty($sociaty_to_user_id)) {
                    app('rongcloud')->whitelist(UID, [$user_id]);
                    app('rongcloud')->whitelist($user_id, [UID]);
                    RedisService::delWhitelist($user_id, UID);
                    RedisService::delWhitelist(UID, $user_id);
                }

            }
        }

        //清除redis的相关记录
        app('redis')->del('user:friend:count:' . UID);
        app('redis')->del('user:friend:count:' . $user_id);
        app('redis')->del('user:fans:count:' . UID);
        app('redis')->del('user:follow:count:' . UID);
        app('redis')->del('user:fans:count:' . $user_id);
        app('redis')->del('user:follow:count:' . $user_id);

        return Response::sendData(true);
    }

    public function getUserPrivateMobile()
    {
        $mobile = app('repo_user')->getUserMobile(UID);
        $mobile_hide = substr_replace($mobile, '****', 3, 4);

        return Response::sendData(['mobile' => $mobile_hide, 'mobile_all' => $mobile]);
    }

    /**
     * @api {get} /api/search/location 搜索位置
     * @apiVersion 0.1.0
     * @apiName 搜索位置
     * @apiGroup 用户模块
     *
     * @apiParam {numeric} lat  定位的纬度,可选
     * @apiParam {numeric} lng  定位的经度,可选
     * @apiParam {string} search  搜索的位置,必传
     *
     * @apiSuccessExample 成功响应
     *
     *  说明:此接口用于搜索地理位置时根据当前定位的经纬度进行附近位置推荐,用户选择某位置后,将该位置的经纬度传给后端
     *
     * {
     * "code": 200,
     * "data": [
     * {
     * "name(地点名称)": "西湖风景区",
     * "location(定位位置)": {
     * "lat": 30.228932,
     * "lng": 120.12792
     * }
     * },
     * {
     * "name": "西湖文化广场",
     * "location": {
     * "lat": 30.28362,
     * "lng": 120.170646
     * }
     * }
     * ]
     * }
     *
     */
    public function searchLocationArea(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'lat' => 'filled|numeric',
                'lng' => 'filled|numeric',
                'search' => 'required|min:1',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $lat = $request->input('lat', 0);
        $lng = $request->input('lng', 0);

        $location = '';
        if (!empty($lat) && !empty($lng)) {
            $location = $lat . ',' . $lng;
        }

        $search = $request->input('search', '');

        $res = MapAreaService::placeSuggestion($search, $location);

        return Response::sendData($res);
    }
}
